//
//  VRLiveLanguageView.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/18.
//

import UIKit

class VRLiveLanguageView: UIView, SAAnimationDelegate, SAAnimationProtocol {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 300.0)
    }
    
    func animationContainerViewStyle() -> SAAnimationViewStyle {
        .bottom
    }
    
    private var pickerView          = UIPickerView()
    private let sureButton          = c_make_ex(UIButton.self)
        .color("FFFC00").text("Done").font(18.font_bold).end()
    private let cancelButton        = c_make_ex(UIButton.self)
        .color(.white).text("Cancel").font(18.font_bold).end()
    private let data = ["English","Español","Português","Deutsch","العربية","Italiano","Le français","Filipino"]
    var selStr = ""
    var didChooseCompletion: ((_ dateStr: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#1F1E18".toColor
        
        subviews{
            sureButton
            cancelButton
            pickerView
        }
        
        sureButton.trailing(15).top(15)
        cancelButton.leading(15).top(15)
        pickerView.leading(15).width(.screenWidth-30).height(250).top(55)
        
        pickerView.delegate = self
        pickerView.dataSource = self
        
        sureButton.addTarget(self, action: #selector(clickDoneButton), for: .touchUpInside)
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
    }
    
//    //日期选择器响应方法
//    @objc func dateChanged(){
//        //更新提醒时间文本框
//        dateStr = datePicker.date.toString()
//    }
    @objc private func clickDoneButton(){
        
        if !self.anySubViewScrolling(view: pickerView) {
            guard let closure = self.didChooseCompletion else { return }
            saAnimate.animation(dismiss: self)
            closure(selStr)
        }
    }
    @objc private func clickCancelButton(){
        saAnimate.animation(dismiss: self)
    }
    
    func anySubViewScrolling(view: UIView) -> Bool {
        
        if view.isKind(of: UIScrollView.self) {
            let scrV = view as! UIScrollView
            if scrV.isDragging || scrV.isDecelerating {
                return true
            }
        }
        for subV in view.subviews {
            if self.anySubViewScrolling(view: subV){
                return true
            }
        }
        return false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension VRLiveLanguageView: UIPickerViewDelegate, UIPickerViewDataSource {
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        1
    }
        
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return data.count
    }
    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return data[row]
//    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        self.selStr = data[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 45.0 // 设置行高为50
    }
    
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let title = data[row] // 替换成你的标题
        let attributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.white, // 设置选中颜色
            .font: 16.font_medium! // 设置字体大小
        ]
        return NSAttributedString(string: title, attributes: attributes)
    }

}
